package amushen.module;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import amushen.base.BaseBO;
import amushen.base.DBUtil;

/**
 * login module
 * @author shen.n
 *
 */
public class Login extends BaseBO {

	/**
	 * check username and password
	 */
	public Object defaultMethod(Map paras, String user, InputStream is,DBUtil db) {	
		String username=(String)paras.get("username");
		String password=(String)paras.get("password");
		if(password==null)password="";
		
		List list=db.query("Login.query", new Object[]{username});
		if(list.size()<1){
			return "No Such User.";
		}
		String userId=(String)(((Object[])list.get(0))[0]);
		String pass=(String)(((Object[])list.get(0))[1]);
		if(pass==null)pass="";
		if(pass.equals(password)){
			getSession().setAttribute("UserName", username);
			getSession().setAttribute("UserId", userId);
		}else{
			return "Password wrong.";
		}
		return "Success!";
	}
	
	/**
	 * return this user's rights,
	 * to create menu
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object getUserRights(Map paras, String user,InputStream is,DBUtil db){
		JSONArray ret=new JSONArray();
		String username=(String)getSession().getAttribute("UserName");
		if(username!=null){
			List list=db.query("Login.rights", new Object[]{username});
			JSONArray pobj=null;			
			JSONObject jobj=null;
			for(int i=0;i<list.size();i++){
				try{
					Object[] row=(Object[])list.get(i);
//					int id=Integer.parseInt(((String)row[0]));
					String label=(String)row[1];
					int parentId=((Integer)row[2]).intValue();
					String url=(String)row[3];
					jobj=new JSONObject();
					jobj.put("label", label);
					jobj.put("url", url);
					if(parentId==0){
						//current item is a child item;
						pobj=new JSONArray();
						jobj.put("children", pobj);
						ret.put(jobj);
					}else{					
						pobj.put(jobj);
					}					
				}catch(JSONException jsone){
					jsone.printStackTrace();				
				}				
			}
		}
		return ret.toString();
	}
	
	public Object getCurrentUserId(Map paras, String user,InputStream is ,DBUtil db){		
		return (String)getSession().getAttribute("UserId");
	}

	public Object logout(Map paras, String user,InputStream is ,DBUtil db){
		getSession().setAttribute("UserName", null);
		getSession().setAttribute("UserId", null);
		return null;
	}
	
	public Object getCurrentUserName(Map paras, String user,InputStream is ,DBUtil db){
		return (String)getSession().getAttribute("UserName");
	}
	
	public Object chgPassword(Map paras, String user,InputStream is ,DBUtil db){
		String old=(String)paras.get("oldPass");
		String pass=(String)paras.get("newPass");
		//System.out.println(pass+","+(String)getSession().getAttribute("UserId"));
		db.update("Login.chgPassword", new Object[]{pass,(String)getSession().getAttribute("UserId")});
		db.commit();
		return "OK";
	}
	
}
